<template>
  <el-form-item label="设置库存" prop="addOrDeductionStockCount" :rules="rules">
    <el-radio-group v-model="addOrDeductionSymbolModel">
      <el-radio-button label="+">增</el-radio-button>
    </el-radio-group>
    <div style="width: 5px"></div>
    <el-input-number
        :step="1"
        step-strictly
        v-model="modelValue"
        :min="1"
    ></el-input-number>
  </el-form-item>
</template>

<script lang="ts" setup>
import {IStoreProductRow} from "@/api/modules/pointsMall";

const addOrDeductionSymbolModel = ref<'+' | '-'>('+')

const props = withDefaults(defineProps<{
  modelValue: number,
  storeRow: IStoreProductRow
}>(), {
  modelValue: 1,
  storeRow: {} as any,
})
const emits = defineEmits(['update:model-value'])

const modelValue = computed({
  set(newValue) {
    emits('update:model-value', newValue)
  },
  get() {
    return props.modelValue || 1
  }
})

const isEmpty = (val: any) => {
  return val === undefined || val === null || val === ''
}

const rules = [{
  required: true,
  trigger: 'blur',
  validator(rule:any, val:any, callback:any) {
    const _addOrDeductionStockCount = Number(addOrDeductionSymbolModel.value + modelValue.value ?? 0)

    if (isEmpty(_addOrDeductionStockCount)) {
      callback(new Error('【库存增减量】不能为空'))
    } else if (_addOrDeductionStockCount < 1) {
      callback(new Error('【库存增减量】不能小于1'))
    } else if (_addOrDeductionStockCount > props.storeRow.beforeStock) {
      callback(new Error('库存【增】量不能大于商家库存'))
    } else {
      callback()
    }
  }
}]
</script>

<style scoped lang="scss">

</style>
